﻿
//获取专家分类树形
function getCategorys(str_url) {
    $.ajax({
        type: "GET",
        url: str_url,
        dataType: "json",
        success: function (data) {
            //初始化专业专家的所属分类
            $.fn.zTree.init($("#treeCategory"), setting, data);
            $.fn.zTree.init($("#frmEdit_expCategory"), setting, data);
        }
    });
}

//获取当前抽取备选记录的项目列表
function getProjectListForThisBakRecord(str_url, bak_record_id) {
    $.ajax({
        type: "GET",
        url: str_url,
        dataType: "json",
        data: { bak_id: bak_record_id },
        success: function (data) {
            $("#bak_projectList").html("");
            //填充关联项目表格
            $('#DataTemplate_ProjectList').tmpl(data).appendTo('#bak_projectList');
        }
    });
}

//设置当前抽取备选项目记录的项目列表
function setProjectListForThisBakRecord(str_url) {
    //获取勾选的项目ID，拼凑IDs
    var strIDs = "";
    $("[name='cbProject']").each(function () {
        if ($(this).attr("checked")) {
            if (strIDs == "") {
                strIDs = $(this).val();
            } else {
                strIDs += "," + $(this).val();
            }
        }
        else {
        }


    })

    //获取当前抽取备选记录ID
    var bak_id = $("#bakID").val();

    $.ajax({
        type: "GET",
        url: str_url,
        dataType: "json",
        data: { bak_record_id: bak_id, bak_record_project_ids: strIDs },
        success: function (data) {
            if (data.ISOK) {
                //刷新勾选记录
                getProjectListForThisBakRecord(strUrlgetThisRecordProjectLists, bak_id);
                alertMSG("保存关联项目！");
            } else {
                alertMSG("系统错误！" + data.MESSAGE);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

//设置当前抽取备选项目记录的项目列表
function getDetailsForThisBakRecord(str_url, str_bak_id) {
    $.ajax({
        type: "POST",
        url: str_url,
        dataType: "json",
        data: { bak_id: str_bak_id },
        success: function (data) {
            if (data != null) {
                $("#ExtractDetails").html("");
                //填充抽取备选记录的详细记录列表
                $('#DataTemplate_Details').tmpl(data).appendTo('#ExtractDetails');
            } else {
                alertMSG("系统错误！" + data.MESSAGE);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

//拼凑抽取选项条件，获取备选专家列表
function getExpertBXs(str_url) {
    //根据专家分类勾选状态，获取专家的分类字符串，以逗号隔开
    var treeObj = $.fn.zTree.getZTreeObj("treeCategory");
    var nodes = treeObj.getCheckedNodes(true);
    var strIDs = "";
    if (nodes != null && nodes.length > 0) {
        for (var i = 0, l = nodes.length; i < l; i++) {
            var halfCheck = nodes[i].getCheckStatus();
            if (!halfCheck.half) {
                if (strIDs == "") {
                    strIDs = nodes[i].id;
                } else {
                    strIDs += "," + nodes[i].id;
                }
            }
        }
    }

    //拼凑查询条件
    var qParam = {
        qName: $("#qName").val(),
        qProfessional: $("#qProfessional").val(),
        qCategoryIDs: strIDs,
        qStatus: "在用",
        recordCount: 0,
        pageCount: 0,
        pageSize: 10,
        pageIndex: 0
    }

    //根据条件查询
    $.ajax({
        type: "POST",
        url: str_url,
        dataType: "json",
        data: JSON.stringify(qParam),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data.ISOK) {
                $("#ExpertBXs").html("");
                //将数据填充表格模板
                $("#DataTemplate_ExpertBX").tmpl(data.experts).appendTo("#ExpertBXs");
                if (data.recordCount > 20) {
                    $("#expertBXsInfo").html("本次查询共获取专家信息" + data.recordCount + "个！当前只显示最前面的20位专家信息。");
                } else {
                    $("#expertBXsInfo").html("本次查询共获取" + data.recordCount + "专家信息！");
                }

            } else {
                $("#expertBXs").html("");
                $("#expertBXsInfo").html("查询专家错误！请确认查询条件是否正确");
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alertMSG(textStatus);
        }
    });
}

//根据专家ID，获取专家信息，放入已选专家列表中
function saveExpertAsDetail(str_url, str_expert_id) {
    //获取当前抽取备选记录ID
    var str_bak_id = $("#bakID").val();

    $.ajax({
        type: "POST",
        url: str_url,
        dataType: "json",
        data: { bak_id: str_bak_id, exp_id: str_expert_id },
        success: function (data) {
            if (data.ISOK) {
                //保存详细记录成功，刷新详细记录列表
                getDetailsForThisBakRecord(strUrlgetThisRecordDetails, str_bak_id);
            } else {
                alertMSG("系统错误！" + data.MESSAGE);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

//删除抽取备选记录的一条详细记录
function deleteADetail(str_url, str_detail_id) {
    //获取当前抽取备选记录ID
    var str_bak_id = $("#bakID").val();

    //删除
    $.ajax({
        type: "POST",
        url: str_url,
        dataType: "json",
        data: { detail_id: str_detail_id },
        success: function (data) {
            if (data.ISOK) {
                //保存详细记录成功，刷新详细记录列表
                getDetailsForThisBakRecord(strUrlgetThisRecordDetails, str_bak_id);
            } else {
                alertMSG("系统错误！" + data.MESSAGE);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

//显示弹出窗体来编辑详细记录
function showFormToEditADetail(str_detail_id) {
    //获取详细记录信息，显示编辑窗
    $.ajax({
        type: "POST",
        url: strUrlgetThisDetail,
        dataType: "json",
        data: { detail_id: str_detail_id },
        success: function (data) {
            if (data != null) {
                $("#frmEdit_detailId").val(data.Id);
                $("#frmEdit_expId").val(data.expId);
                $("#frmEdit_expName").html(data.expName);
                $("#frmEdit_expCompany").html(data.expCompany);
                $("#frmEdit_expMobile").val(data.expMobile);
                $("#frmEdit_expProfession").val(data.expProfession);

                $("input[name='frmEdit_is_selected'][type='radio'][value=" + data.is_selected + "]").attr("checked", true);

                $("#frmEdit_un_selected_msg").html(data.un_selected_msg);

                var category_ids = data.expCategoryIds.split(",");
                var treeObj = $.fn.zTree.getZTreeObj("frmEdit_expCategory");
                if (treeObj != null) {
                    for (var i = 0; i < category_ids.length; i++) {
                        var nodes = treeObj.getNodesByParam("id", category_ids[i], null);
                        if (nodes.length > 0) {
                            treeObj.checkNode(nodes[0], true, true);
                        }
                    }

                    //显示编辑窗
                    $('#FORM_EditDetail').dialog('open');
                }
            } else {
                alertMSG("系统错误！" + data.MESSAGE);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

//保存当前修改窗体中的内定记录
function saveThisDetail() {
    //获取是否参加评审选项
    var is_sel = 0;
    is_sel = $("input[name='frmEdit_is_selected'][type='radio']:checked").val();

    //获取勾选的专家分类
    var treeObj = $.fn.zTree.getZTreeObj("frmEdit_expCategory");
    var nodes = treeObj.getCheckedNodes(true);
    var strIDs = "";
    if (nodes != null && nodes.length > 0) {
        for (var i = 0, l = nodes.length; i < l; i++) {
            var halfCheck = nodes[i].getCheckStatus();
            if (!halfCheck.half) {
                if (strIDs == "") {
                    strIDs = nodes[i].id;
                } else {
                    strIDs += "," + nodes[i].id;
                }
            }
        }
    }

    //获取详细记录信息，保存记录，关闭编辑窗
    var _detail = new Object;
    _detail.Id = $("#frmEdit_detailId").val();
    _detail.is_selected = is_sel;
    _detail.un_selected_msg = $("#frmEdit_un_selected_msg").html();
    _detail.expProfession = $("#frmEdit_expProfession").val();
    _detail.expMobile = $("#frmEdit_expMobile").val();
    _detail.expCategoryIds = strIDs;

    _detail.order_flag = 0;
    _detail.expId = "";
    _detail.expName = "";
    _detail.expCategoryNames = "";
    _detail.expCompany = "";
    _detail.expPhone = "";
    _detail.expTitle = "";

    $.ajax({
        type: "POST",
        url: strUrlsaveThisDetail,
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(_detail),
        success: function (data) {
            $('#FORM_EditDetail').dialog("close");

            if (data != null) {
                if (!data.ISOK) {
                    alertMSG("系统错误！错误代码为：" + data.MESSAGE);
                }
            } else {
                alertMSG("系统错误！返回为空状态");
            }

            //刷新当前详细记录信息
            getDetailsForThisBakRecord(strUrlgetThisRecordDetails, $("#bakID").val());
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}